package service.UserManagement;

import entity.UserInfForEn;
import entity.UserInfForStu;
import mapper.UserManagement.UserInfForEnMapper;
import mapper.UserManagement.UserInfForStuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import entity.User;
import mapper.UserManagement.UserMapper;

import java.util.LinkedHashMap;

@Service
public class UserService {
    //此处变量是接口类型，不加@Autowired
    private UserMapper userMapper;

    @Autowired
    private UserInfForStuMapper userInfForStuMapper;

    @Autowired
    private UserInfForEnMapper userInfForEnMapper;

    @Autowired
    public UserService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    public boolean login(User user) {
//        System.out.println("user:"+user);
        User u = userMapper.findUser(user);
//        System.out.println("u:"+u);
        if (u != null)
            return true;
        else {
            return false;
        }
    }

    public boolean updateUserIcon(String icon,String userId){
        if(userMapper.updateUserIcon(icon,userId)==1){
            return true;
        }
        return false;
    }

    @Transactional//增删改操作要添加@Transactional事务注解,这样程序无需添加提交、回滚等事务操作(将由spring接管)
    public boolean register(User user) {
        int cnt = userMapper.addUser(user);
        if (cnt != 0)
            return true;
        else {
            return false;
        }
    }

    @Transactional//增删改操作要添加@Transactional事务注解,这样程序无需添加提交、回滚等事务操作(将由spring接管)
    public boolean BindUserInfForStu(UserInfForStu userInfForStu) {
        int cnt = userInfForStuMapper.BindUserInfForStuMapper(userInfForStu);
        if (cnt != 0)
            return true;
        else {
            return false;
        }
    }

    @Transactional//增删改操作要添加@Transactional事务注解,这样程序无需添加提交、回滚等事务操作(将由spring接管)
    public boolean BindUserInfForEn(UserInfForEn userInfForEn) {
        int cnt = userInfForEnMapper.BindUserInfForEnMapper(userInfForEn);
        if (cnt != 0)
            return true;
        else {
            return false;
        }
    }

    public User findUser(String userId) {
        User u = userMapper.findUserByUserId(userId);
            return u;
    }

    @Transactional
    public boolean addUserInformationId(String userInformationId,String userId){
        int flag = userMapper.addUserInformationId(userInformationId,userId);
        if (flag == 1){
            return true;
        }
        return false;
    }

    public UserInfForStu showUser(String phoneNumber){
        return userMapper.showUser(phoneNumber);
    }

    public LinkedHashMap<String,Object> showEnr(String phoneNumber){
        return userMapper.showEnr(phoneNumber);
    }
}